package com.itmuch.cloud.study.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itmuch.cloud.study.entity.UserEntity;
import com.itmuch.cloud.study.repository.UserRepository;
import com.itmuch.cloud.study.user.domain.User;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserRepository userRepository;

	@Override
	public User create(User user) {
		UserEntity m = new UserEntity();
		BeanUtils.copyProperties(user, m);
		userRepository.save(m);
		BeanUtils.copyProperties(m, user);
		return user;
	}

	@Override
	public User get(String id) {
		UserEntity findOne = this.userRepository.findOne(id);
		User u = null;
		if (findOne != null) {
			u = new User();
			BeanUtils.copyProperties(findOne, u);
		}
		return u;
	}

	@Override
	public User update(String id, User user) {
		UserEntity m = userRepository.findOne(id);
		if (m != null) {
			BeanUtils.copyProperties(user, m);
			userRepository.save(m);
			BeanUtils.copyProperties(m, user);
		}

		return user;
	}

	@Override
	public User delete(String id) {
		UserEntity m = userRepository.findOne(id);
		if (m != null) {
			User user = new User();
			userRepository.save(m);
			BeanUtils.copyProperties(m, user);
			return user;
		}

		return null;
	}

}
